package com.cuz.daileetcode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Day51 {


    public class StackMinItemGetAble {
        private Stack<Integer> dataStack;
        private Stack<Integer> curMinStack;

        public StackMinItemGetAble() {
            dataStack = new Stack<>();
            curMinStack = new Stack<>();
        }

        private boolean isDataStackEmpty() {
            return dataStack.isEmpty();
        }

        private boolean isCurMinStackEmpty() {
            return curMinStack.isEmpty();
        }

        public void push(int item) {
            dataStack.push(item);
            if (isDataStackEmpty()) {
                curMinStack.push(item);
            } else {
                Integer curMin = curMinStack.peek();
                if (curMin > item) {
                    curMinStack.push(item);
                } else {
                    curMinStack.push(curMin);
                }
            }
        }

        public int pop() {
            curMinStack.pop();
            return dataStack.pop();
        }

        public int getMin() {
            if (curMinStack.isEmpty()) {
                throw new UnsupportedOperationException("当前栈不包含元素");
            }
            return curMinStack.peek();
        }
    }

    public class QueueStack {
        private Queue<Integer> queue1 = new LinkedList<>();
        private Queue<Integer> queue2 = new LinkedList<>();

        public void push(int item) {
            noEmptyQueueOfThis().offer(item);
        }

        public int pop() {
            if (bothEmpty()) {
                throw new UnsupportedOperationException();
            }
            Queue<Integer> noEmpty = noEmptyQueueOfThis();
            Queue<Integer> empty = emptyQueueOfThis();
            while (noEmpty.size() != 1) {
                empty.offer(noEmpty.poll());
            }
            return noEmpty.poll();
        }

        private boolean bothEmpty() {
            return queue1.isEmpty() && queue2.isEmpty();
        }

        private Queue<Integer> noEmptyQueueOfThis() {
            if (bothEmpty()) {
                return queue1;
            }
            if (queue2.isEmpty()) {
                return queue1;
            }
            return queue2;
        }

        private Queue<Integer> emptyQueueOfThis() {
            if (noEmptyQueueOfThis().equals(queue1)) {
                return queue2;
            }
            return queue1;
        }
    }


}
